package com.dy.分类.递归回溯分治.子集二;

import java.util.*;

//去重，先排序 sort，再用SET
public class Solution {
    public List<List<Integer>> subsets(int[] nums) {
        Arrays.sort(nums);
   //     List<List<Integer>> res = new LinkedList<>();
        List<Integer> list = new LinkedList<>();
        Set<List<Integer>> set = new HashSet<>();
        set.add(list);
        digui(0, nums, list, set);
        return new LinkedList<>(set);
    }

    void digui(int i, int[] nums,   List<Integer> list, Set<List<Integer>> set) {
        //如果i==nums.length return
        if (i == nums.length) {
            return;
        }

        list.add(nums[i]);
        if (!set.contains(list)) {
            List a= new ArrayList<Integer>(list);
            set.add(a);
        }
        digui(i + 1, nums,  list, set);
        //回溯，删除最后一个元素，回到原来的样子，再递归一次
        list.remove(list.size() - 1);
        digui(i + 1, nums,  list, set);
    }

    public static void main(String[] args) {
        int nums[]={1,2,2};
        Solution solution = new Solution();
        solution.subsets(nums);
    }
}